<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
import 'echarts/map/js/china.js'

require('echarts/theme/macarons') // echarts theme

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '600px'
    }
  },
  data() {
    return {
      chart: null,
      chartData: {
        mapData: []
      }
    }
  },
/*  mounted() {
    getMapChartData().then(res => {
      this.chartData.mapData = res.visitsData
      this.initChart()
    })
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },*/
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        title: {
          text: '用户归属地地图'
        },
        calculable: true,
        series: [{
          name: '近30天流量',
          type: 'map', // 告诉echarts 渲染地图
          map: 'china',
          data: this.chartData.mapData,
          label: {
            show: true, // 显示各个省份名称
            fontSize: 8
          },
          itemStyle: {
            areaColor: '#fff' // 区域的背景颜色
          },
          emphasis: {
            // 控制鼠标滑过时的高亮样式
            itemStyle: {
              areaColor: '#c7fffd'
            }
          },
          zoom: 1.2 // 控制地图的大小
        }],
        visualMap: [{
          type: 'piecewise',
          show: true,
          splitNumber: 6,
          pieces: [{
            min: 10000
          }, // 不指定 max，表示 max 为无限大（Infinity）。
          {
            min: 1000,
            max: 9999
          },
          {
            min: 100,
            max: 999
          },
          {
            min: 10,
            max: 99
          },
          {
            min: 1,
            max: 9
          },
          {
            min: 0,
            max: 0
          }
          ],
          // align:'right' // 默认是left
          inRange: {
            symbol: 'rect',
            color: ['#fff', '#ffaa85', '#ff7b69', '#cc2929', '#8c0d0d', '#660208']
          },
          itemHeight: 10,
          itemWidth: 20
        }],
        tooltip: {
          formatter: function(params, ticket, callback) {
            if (params.value) {
              return params.seriesName + '<br/>' + params.name + ' : ' + params.value
            } else {
              return params.seriesName + '<br/>' + params.name + ' : ' + '0'
            }
          }
        }
      })
    }
  }
}
</script>
